package com.central.scheduler.dao.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.central.common.handler.ListPropToStringHandler;
import com.central.common.model.SuperEntity;
import com.central.scheduler.common.enums.ReleaseState;
import com.central.common.properties.Property;
import com.central.scheduler.dao.entity.TaskNodeParam;
import com.central.scheduler.dao.handler.ListTaskNodeParamToStringHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;

import java.util.*;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.annotation.FieldStrategy.NOT_NULL;

/**
 * @author Tindy
 * @date 2021/10/31
 * @describe
 */
@Data
@TableName(value = "sch_process_definition",autoResultMap = true)
public class ProcessDefinition extends SuperEntity {
    /**
     * code
     */
    private long code;

    /**
     * name
     */
    private String name;

    /**
     * version
     */
    private int version;

    /**
     * release state : online/offline
     */
    private ReleaseState releaseState;

    /**
     * project id
     */
    private Long proId;
    private String proName;

    /**
     * description
     */
    private String description;

    /**
     * user defined parameter list
     */
    @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = NOT_NULL, typeHandler = ListPropToStringHandler.class)
    private List<Property> globalParams;

    /**
     * user define parameter map
     */
    @TableField(exist = false)
    private Map<String, String> globalParamMap;

    /**
     * locations array for web
     */
    @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = NOT_NULL, typeHandler = ListTaskNodeParamToStringHandler.class)
    private List<TaskNodeParam> taskNodeParams;

    /**
     * schedule release state : online/offline
     */
    @TableField(exist = false)
    private ReleaseState scheduleReleaseState;
    /**
     * schedule start time
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @TableField(exist = false)
    private Date scheduleStartTime;

    /**
     * schedule end time
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @TableField(exist = false)
    private Date scheduleEndTime;

    /**
     * crontab expression
     */
    @TableField(exist = false)
    private String scheduleCrontab;


    /**
     * process warning time out. unit: minute
     */
    private int timeout;

    private boolean isTmp;

    /**
     * warningGroupId
     */
    @TableField
    private long warningGroupId;

    @TableField(exist = false)
    private List<ProcessTaskRelationLog> taskRelationList;

    public ProcessDefinition() {
    }

    public ProcessDefinition(long proId,
                             String proName,
                             String name,
                             long code,
                             ReleaseState releaseState,
                             String description,
                             List<Property> globalParams,
                             List<TaskNodeParam> taskNodeParams,
                             int timeout,
                             boolean isTmp) {
        set(proId, proName,name, releaseState,description, globalParams, taskNodeParams, timeout,isTmp);
        this.code = code;
    }

    public void set(Long proId,
                    String proName,
                    String name,
                    ReleaseState releaseState,
                    String description,
                    List<Property> globalParams,
                    List<TaskNodeParam> taskNodeParams,
                    int timeout,
                    boolean isTmp) {
        this.proId=proId;
        this.proName=proName;
        this.name = name;
        this.releaseState=releaseState;
        this.description = description;
        this.globalParams = globalParams;
        this.taskNodeParams = taskNodeParams;
        this.timeout = timeout;
        this.isTmp=isTmp;
    }

    public Map<String, String> getGlobalParamMap() {
        if(globalParams!=null){
            globalParamMap = globalParams.stream().collect(Collectors.toMap(Property::getProp, Property::getValue));
        }else {
            globalParamMap =new HashMap<>();
        }
        return globalParamMap;
    }
}
